//
//  HelpEachOtherViewController.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/6.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "HelpEachOtherViewController.h"
#import "HelpEachOtherTableViewCell.h"
#import "JoinDetailViewController.h"
#import "UpdateDetailViewController.h"
#import "MessageViewController.h"
#import "DashesLineView.h"
#import <sys/utsname.h>
#import "PersonalInfoViewController.h"

@interface HelpEachOtherViewController ()<UITableViewDelegate, UITableViewDataSource, SDCycleScrollViewDelegate>
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UILabel * joinedPeopleLab;//已加入人数
@property (nonatomic, strong)UILabel * transferedMoneyLab;//已划拨款
//@property (nonatomic, strong)NSMutableArray * cellData;
@property (nonatomic, strong)NSString * phoneVersion;//手机版本
@property (nonatomic, strong)NSString * phoneModel;//手机型号
@property (nonatomic, strong)NSMutableDictionary * homeDataDic;
@property (nonatomic, strong)NSMutableArray * bannerURLArr;//轮播图地址数组
@property (nonatomic, strong)NSMutableArray * planDataArr;//互助计划内容数组
@property (nonatomic, assign)int currentPage;
@property (nonatomic, strong)UILabel * runDaysLab;//运营天数
@end

@implementation HelpEachOtherViewController (networking)
- (void)getHomeData{
    NSDictionary *parameters = @{
                                 @"page":@(self.currentPage),
                                 @"size":@(8),
                                 @"userId":@(0)
                                 };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"plan/getHomeData" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"msg"] isEqualToString:@"success"]) {
            self.homeDataDic = responseObject[@"list"];
            //轮播图
            NSMutableArray *cycleScrollViewArr = [NSMutableArray array];
            for (NSDictionary * dic in self.homeDataDic[@"banner"]) {
                NSString * pic = [NSString stringWithFormat:@"%@",dic[@"img"]];
                [cycleScrollViewArr addObject:pic];
                [self.bannerURLArr addObject:dic[@"link"]];
            }
            self.cycleScrollView.imageURLStringsGroup = cycleScrollViewArr;
            //互助计划内容
            if (self.currentPage == 1) {
                [self.planDataArr removeAllObjects];
            }
            [self.planDataArr addObjectsFromArray:self.homeDataDic[@"plan"]];
            [self.tableView reloadData];
            self.joinedPeopleLab.text = self.homeDataDic[@"userCount"];//加入人数
            self.transferedMoneyLab.text = self.homeDataDic[@"money"];//已划拨款
            self.runDaysLab.text = [NSString stringWithFormat:@"博帮平台已运营%@天", self.homeDataDic[@"yyday"]];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end

@implementation HelpEachOtherViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.currentPage = 1;
    [self getHomeData];//首页数据
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self.view addSubview:self.tableView];
    //右侧消息按钮
    UIButton * naviRightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40/WIDTH_6_SCALE, 20/WIDTH_6_SCALE)];
    [naviRightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [naviRightBtn setTitle:@"消息" forState:UIControlStateNormal];
    [naviRightBtn addTarget:self action:@selector(naviRightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    naviRightBtn.titleLabel.font = DEF_FontSize_12;
    UIBarButtonItem * rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:naviRightBtn];
    self.navigationItem.rightBarButtonItem = rightButtonItem;}
#pragma mark - init
- (NSMutableDictionary *)homeDataDic{
    if (!_homeDataDic) {
        _homeDataDic = @{}.mutableCopy;
    }return _homeDataDic;
}
- (NSMutableArray *)bannerURLArr{
    if (!_bannerURLArr) {
        _bannerURLArr = @[].mutableCopy;
    }return _bannerURLArr;
}
- (NSMutableArray *)planDataArr{
    if (!_planDataArr) {
        _planDataArr = @[].mutableCopy;
    }return _planDataArr;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TABBAR_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = self.headerView;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[HelpEachOtherTableViewCell class] forCellReuseIdentifier:NSStringFromClass([HelpEachOtherTableViewCell class])];
        _tableView.rowHeight = 108/WIDTH_6_SCALE;
        __weak typeof (self)weakSelf = self;
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            weakSelf.currentPage ++;
            [self getHomeData];
        }];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.currentPage = 1;
            [self getHomeData];
        }];
    }return _tableView;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 287/WIDTH_6_SCALE)];
        _headerView.backgroundColor = [UIColor grayColor];
        //轮播图
        self.cycleScrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150/WIDTH_6_SCALE)];
//        self.cycleScrollView.backgroundColor = [UIColor blueColor];
        self.cycleScrollView.placeholderImage = [UIImage imageNamed:@"maself_bg"];
        self.cycleScrollView.delegate = self;
        self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        self.cycleScrollView.currentPageDotColor = [UIColor whiteColor];
        self.cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleToFill;
        self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        __weak typeof (self)weakSelf = self;
        self.cycleScrollView.clickItemOperationBlock = ^(NSInteger index) {
            NSLog(@">>>>>  %ld", (long)index);
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:weakSelf.bannerURLArr[index]]];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:weakSelf.bannerURLArr[index]] options:nil completionHandler:nil];
        };
        [_headerView addSubview:self.cycleScrollView];
        //数字白色背景
        UIView * whiteBGView = [[UIView alloc] initWithFrame:CGRectMake(0, self.cycleScrollView.bottom, SCREEN_WIDTH, 87/WIDTH_6_SCALE)];
        whiteBGView.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:whiteBGView];
        //已加入人数
        self.joinedPeopleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 22/WIDTH_6_SCALE, SCREEN_WIDTH/2, 25/WIDTH_6_SCALE)];
        self.joinedPeopleLab.text = @"11111111";
        self.joinedPeopleLab.font = DEF_FontSize_16;
        self.joinedPeopleLab.textAlignment = NSTextAlignmentCenter;
        self.joinedPeopleLab.textColor = getColor(greenBGColor);
        [whiteBGView addSubview:self.joinedPeopleLab];
        UILabel * joinPeopleTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, self.joinedPeopleLab.bottom + 8/WIDTH_6_SCALE, SCREEN_WIDTH/2, 17/WIDTH_6_SCALE)];
        joinPeopleTitleLab.text = @"已加入人数(人)";
        joinPeopleTitleLab.font = DEF_FontSize_12;
        joinPeopleTitleLab.textAlignment = NSTextAlignmentCenter;
        joinPeopleTitleLab.textColor = getColor(@"9b9b9b");
        [whiteBGView addSubview:joinPeopleTitleLab];
        //已划拨款
        self.transferedMoneyLab = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2, self.joinedPeopleLab.top, self.joinedPeopleLab.width, self.joinedPeopleLab.height)];
        self.transferedMoneyLab.text = @"11111111";
        self.transferedMoneyLab.font = DEF_FontSize_16;
        self.transferedMoneyLab.textAlignment = NSTextAlignmentCenter;
        self.transferedMoneyLab.textColor = getColor(greenBGColor);
        [whiteBGView addSubview:self.transferedMoneyLab];
        UILabel * transferedMoneyTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2, joinPeopleTitleLab.top, joinPeopleTitleLab.width, joinPeopleTitleLab.height)];
        transferedMoneyTitleLab.text = @"已划拨互助款(元)";
        transferedMoneyTitleLab.font = DEF_FontSize_12;
        transferedMoneyTitleLab.textAlignment = NSTextAlignmentCenter;
        transferedMoneyTitleLab.textColor = getColor(@"9b9b9b");
        [whiteBGView addSubview:transferedMoneyTitleLab];
        //中间竖线
        UIView * labelLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 28/WIDTH_6_SCALE, 1/WIDTH_6_SCALE, whiteBGView.height - 28/WIDTH_6_SCALE - 21/WIDTH_6_SCALE)];
        labelLineView.centerX = whiteBGView.centerX;
        labelLineView.backgroundColor = getColor(@"eeeeee");
        [whiteBGView addSubview:labelLineView];
        //灰色间隔
        UIView * grayView = [[UIView alloc] initWithFrame:CGRectMake(0, whiteBGView.bottom, SCREEN_WIDTH, 9/WIDTH_6_SCALE)];
        grayView.backgroundColor = getColor(@"f5f5f5");
        [_headerView addSubview:grayView];
        //组头白色背景
        UIView * sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, grayView.bottom, SCREEN_WIDTH, _headerView.height - grayView.bottom - 1/WIDTH_6_SCALE)];
        sectionHeaderView.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:sectionHeaderView];
        //组头标题
        UILabel * sectionTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(15/WIDTH_6_SCALE, 0, 100/WIDTH_6_SCALE, sectionHeaderView.height)];
        sectionTitleLab.textColor = [UIColor blackColor];
        sectionTitleLab.textAlignment = NSTextAlignmentLeft;
        sectionTitleLab.text = @"加入互助";
        sectionTitleLab.font = DEF_FontSize_14;
        [sectionHeaderView addSubview:sectionTitleLab];
        //运营天数
        int runDaysLabW = SCREEN_WIDTH - 100/WIDTH_6_SCALE - 30/WIDTH_6_SCALE;
        UILabel * runDaysLab = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 15/WIDTH_6_SCALE - runDaysLabW, 0, runDaysLabW, sectionHeaderView.height)];
        runDaysLab.textColor = [UIColor blackColor];
        runDaysLab.textAlignment = NSTextAlignmentRight;
        runDaysLab.text = @"博帮平台已运营0天";
        runDaysLab.font = DEF_FontSize_14;
        runDaysLab.textColor = getColor(@"d3a85c");
        [sectionHeaderView addSubview:runDaysLab];
        self.runDaysLab = runDaysLab;
        //底部横线
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, sectionHeaderView.bottom, SCREEN_WIDTH, 1/WIDTH_6_SCALE)];
        lineView.backgroundColor = getColor(@"eeeeee");
        [_headerView addSubview:lineView];
    }return _headerView;
}
#pragma mark - action
- (void)naviRightBtnClick:(UIButton *)sender{
//    MessageViewController * vc = [[MessageViewController alloc] init];
//    vc.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:vc animated:YES];
    PersonalInfoViewController * vc = [[PersonalInfoViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HelpEachOtherTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HelpEachOtherTableViewCell class]) forIndexPath:indexPath];
    cell.model = self.planDataArr[indexPath.row];
    cell.joinPageBlock = ^(NSString * cellID) {
        JoinDetailViewController * vc = [[JoinDetailViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.projectID = cellID;
        [self.navigationController pushViewController:vc animated:YES];
    };
    cell.updatePageBlock = ^(NSString * cellID) {
        UpdateDetailViewController * vc = [[UpdateDetailViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.projectID = cellID;
        [self.navigationController pushViewController:vc animated:YES];
    };
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.planDataArr.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
@end
